/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  MainHuaTiFragment.java   
 * @Package com.jiyi.activity.main   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-21 下午5:27:24   
 * @version V1.0     
 */
package com.jiyi.activity.main;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jiyi.adapter.DividerItemDecoration;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.bean.main.TopicCatBean;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainHuaTiFragment extends Fragment {

	@BindView(R.id.id_stickynavlayout_innerscrollview)
	RecyclerView mainList;

	CommonAdapter mCommonAdapter;
	List<TopicCatBean> huaTiList = new ArrayList<TopicCatBean>();

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_tab, null, false);
		ButterKnife.bind(this, view);
		init();
		return view;
	}

	/**
	 * @author: HYM
	 * @Title: init
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-21
	 */
	private void init() {
		setAdater();
		getData();
	}


	/**   
	 * @author: HYM 
	 * @Title: getData   
	 * @Description: TODO(这里用一句话描述这个方法的作用)    void 
	 * @date: 2017-7-22
	 */  
	private void getData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		

		VolleyRequest.StringRequestPost(getContext(),
				WebApi.MAIN_TOPIC_CAT_LIST, "", params, new StringInterface(
						getContext()) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								String data = obj.getString("data");
								JSONObject dataJson = new JSONObject(data);
								String topicJson = dataJson
										.getString("topic_act");
								List<TopicCatBean> temp = new Gson().fromJson(
										topicJson,
										new TypeToken<List<TopicCatBean>>() {
										}.getType());
								huaTiList.clear();
								huaTiList.addAll(temp);
								mCommonAdapter.notifyDataSetChanged();

							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}

	private void setAdater() {
		// 设置布局管理器
		mainList.setLayoutManager(new LinearLayoutManager(getContext()));
		// 添加分割线
		mainList.addItemDecoration(new DividerItemDecoration(getContext(),
				DividerItemDecoration.VERTICAL_LIST, getResources()
						.getDrawable(R.drawable.drvice_5)));
		// 初始化适配器
		mCommonAdapter = new CommonAdapter<TopicCatBean>(getContext(),
				R.layout.item_main_huati, huaTiList) {
			@Override
			protected void convert(
					com.jiyi.adapter.recyclerview.base.ViewHolder holder,
					final TopicCatBean t, int position) {
				holder.setText(R.id.tvHuaTiName, t.getName());
				holder.setText(R.id.tvHuaTiJoinNum, "参与话题 " + t.getCount()
						+ " 人");
				holder.setImage(R.id.ivLogo, t.getIcon());
				holder.setImage(R.id.ivCover, t.getCover());
				holder.setOnClickListener(R.id.root, new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(getActivity(),MainHuaTiDetailActivity.class);
						intent.putExtra("topicCatBean", new Gson().toJson(t));
						startActivity(intent);
					}
				});
			}
		};
		mainList.setAdapter(mCommonAdapter);
	}
}
